/**
 * Created By xiyuan
 * Author server@xiyuan.name (惜缘叛逆)
 * DateTime 2019/3/24 8:26 PM
 * Describe javascript功能描述
 * MIT License http://www.opensource.org/licenses/mit-license.php
 */

/*const fs = require('fs');
// 唯一id
const uuid = require('uuid');
// ini配置文件处理工具
const ini = require('ini');

const config = ini.parse(fs.readFileSync('./bin/frps_full.ini', 'utf-8'))
console.log(config)

fs.writeFileSync('./server_config_modified.json', JSON.stringify(config))*/

const {Client} = require('./index');

// 读取配置
const {state, config} = Client.readConfig();

// 客户端列表
let clientContainers = [];

if (false && state) {
	clientContainers = Client.clientRecover().start();
} else {
	
	// 创建客户端
	const client = Client.createClient({
		// 服务器名称
		name: 'client-1',
		options: {
			// 服务器地址
			"server_addr": "0.0.0.0",
			// 服务器端口
			"server_port": "7000",
			"admin_port": "7400",
		}
	});
	
	clientContainers.push(client);
	
	/*client.add('ssh', 'home-ssh', {
		"local_port": "22",
		"remote_port": "6001",
	})*/
	
	/*client.add('dns', 'home-dns', {
		"local_ip": "8.8.8.8",
		"local_port": "22",
		"remote_port": "6001",
	})*/
	
	/*client.add('static', 'home-static', {
		"remote_port": "6001",
		"plugin_local_path": "/wwwroot/static",
	})*/
	
	/*client.add('web', 'home-web', {
		"local_port": "8080",
		"custom_domains":"web.xiyuan.name"
	})*/
	
	/*client.add('p2p', 'home-p2p', {
		"local_port": 9080
	})*/
	
	/*client.add('p2p', 'home-p2p-visitor', {
		"role":"xiyuan",
		"bind_addr":'1.1.1.1',
		"bind_port":"3000"
	})*/
	
	client.add('port', 'home-port', {
		"range": true,
		"local_port": "999-1007,1200",
		"remote_port": "1000-1008,1200",
	})
	
	let res = client.add('tcp_balanced', 'home-tcp_balanced', {
		"local_port": "1007,1200",
		"remote_port": "1300",
	})
	
	client.start('tcp_balanced', 'home-tcp_balanced');
	
	client.start('port', 'home-port')
	
	// 测试中途更新指定的代理
	client.update('tcp_balanced', 'home-tcp_balanced', {
		"local_port": "1000,1200",
		"remote_port": "1200",
		// "remote_port": "1208",
	});
	
	setTimeout(function () {
		
		// 代理信息
		Client.proxyInfo('client-1','port','home-port').then(res=>{
		// Client.proxyInfo('client-1','tcp_balanced', 'home-tcp_balanced').then(res=>{
			console.log(res,'---')
		})
		// 测试中途停止指定的代理
		// client.stop('tcp_balanced', 'home-tcp_balanced')
		// 测试中途移除指定的代理
		// client.remove('tcp_balanced', 'home-tcp_balanced')
		
	}, 5000)
}

clientContainers.forEach(function (client) {
	client.watchInfo(function (info) {
		// console.log(':::', info);
	})
})

// 启动监听
clientContainers[0].watchStart(function (info) {
	console.log('客户端启动成功')
})

// 删除客户端
// Client.removeClient(clientContainers[0])






